/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.List;
import javax.persistence.EntityManager;
import model.Product;

/**
 *
 * @author hendro
 */
public class ProductDAO {
    private EntityManager em;
    public ProductDAO(){}

    public ProductDAO(EntityManager em) {
        this.em = em;
    }
    public void save(Product prod) {
        if (prod.getId() == null) {
            em.persist(prod);
        } else {
            em.merge(prod);
        }
    }

    public void delete(Product prod) {
        em.remove(prod);
    }

    public Product getProduct(Long id) {
        return em.find(Product.class, id);
    }

    public List<Product> getProducts() {
        return em.createQuery("select p from Product p").getResultList();
    }


}
